<?php
namespace app\admin\controller;
use think\Db;
use app\common\controller\AdminBase;
use think\Config;
class User extends AdminBase{
    public function index(){
        $data=Db::view('user')
                  ->view('auth_group_access','group_id','user.id=auth_group_access.uid')
                  ->view('auth_group','title','auth_group.id=auth_group_access.group_id')
                  ->select();
        $this->assign('list',$data);
        return $this->fetch();
    }
    public function add(){
        $data=Db::name('auth_group')->select();
        $this->assign('list',$data);
        if(request()->isPost()){
            $data=input('post.');  
            $result = $this->validate($data,'user');
            if($result!==true){
                return ['msg'=>$result,'status'=>false];
            }
            $user_data=array(
                'id'=>null,
                'username'=>$data['username'],
                'password'=>md5($data['password']),
                'truename'=>$data['truename'],
                'likename'=>$data['likename'],
                'last_ip'=>$this->getIP(),
                'email'=>$data['email'],
                'qq'=>$data['qq'],
                'tel'=>$data['tel'],
                'status'=>$data['status'],
                'addtime'=>date("Y-m-d H:i",time())
            );      
           
            
            $id=Db::name('user')->insert($user_data,false,true);
            if($id){
                Db::name('auth_group_access')->insert(['uid'=>$id,'group_id'=>$data['group_id']],false,true);
                return ['msg'=>"新增成功！",'status'=>true];
            }else{
                return ['msg'=>"新增失败！",'status'=>false];
            }
        }
        return $this->fetch();
    }
    /*修改*/
    public function edit(){
        $id=input('id');       
        $user_data=Db::name('user')->where('id',$id)->find();
        $this->assign('list', Db::name('auth_group')->select());
        $this->assign('accesslist',Db::name('auth_group_access')->where('uid',$id)->find());
        $this->assign('userlist',$user_data);        
        if(request()->isPost()){
            $data=input('post.');
            $user_data=array(
                'id'=>$data['id'],
                'username'=>$data['username'],                
                'truename'=>$data['truename'],
                'likename'=>$data['likename'],
                'last_ip'=>$this->getIP(),
                'email'=>$data['email'],
                'qq'=>$data['qq'],
                'tel'=>$data['tel'],
                'status'=>$data['status']                
            );
            if(!empty($data['password'])){
                $user_data['password']=md5($data['password']);
            }
            $user=Db::name('user')->update($user_data,false,true);
            $access=Db::name('auth_group_access')->where('uid',$data['id'])->update(['group_id'=>$data['group_id']],false,true);
            if($user||$access){
                 return ['msg'=>"修改成功！",'status'=>true];
            }else{
                $this->error("修改失败！");
               // return ['msg'=>"修改失败！",'status'=>false];
            }
            
        }else{
            return $this->fetch();
        }
       
    }
    /*删除*/
    public function delete(){
        if(request()->isPost()){
            $data=input('id');
            if($data!=session('auth_user.uid')&&config('administrator')!='admin'){
                $group=Db::name('auth_group_access')->where('uid',$data)->delete();		
    		    $user=Db::name('user')->where('id',$data)->delete();
    		    if($group&&$user){
    		        return ['msg'=>"删除成功！",'status'=>true];
    		    }else{
    		        return ['msg'=>"删除失败！",'status'=>false];
    		    }
            }else{
                return ['msg'=>"删除失败！",'status'=>false];
            }
        }
        
    }
}